package com.smart.manager.finance.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import com.smart.common.entities.core.Page;
import com.smart.manager.finance.entities.bodys.RAccountAlipay;
import com.smart.manager.finance.entities.models.FinanceAccountAlipay;
import com.smart.manager.finance.entities.models.query.QFinanceAccountAlipay;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 支付中心-订单管理
 *
 * @author Administrator
 */

@Log4j2
@Service
@RequiredArgsConstructor
public class AccountAlipayRepository {


    public Page<FinanceAccountAlipay> pageList(RAccountAlipay.SearchBody search) {
        QFinanceAccountAlipay query = new QFinanceAccountAlipay();

        Opt.ofBlankAble(search.getAppId()).ifPresent(x -> query.appId.eq(x));
        Opt.ofBlankAble(search.getAppType()).ifPresent(x -> query.appType.eq(x));
        Opt.ofBlankAble(search.getDescription()).ifPresent(x -> query.description.contains(x));
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows()).order().id.desc();
        return new Page<FinanceAccountAlipay>().load(query.findPagedList());
    }

    /**
     * 添加
     *
     * @param form 形式
     */
    public void add(RAccountAlipay.EditBody form) {
        FinanceAccountAlipay record = new QFinanceAccountAlipay().appId.eq(form.getAppId()).findOne();
        Assert.isNull(record, "应用ID已存在");
        FinanceAccountAlipay paymentWechat = BeanUtil.copyProperties(form, FinanceAccountAlipay.class);
        paymentWechat.save();

    }

    /**
     * 更新
     *
     * @param id   id
     * @param form 形式
     */
    public void update(Long id, RAccountAlipay.EditBody form) {
        FinanceAccountAlipay record = new QFinanceAccountAlipay().id.eq(id).findOne();
        Assert.notNull(record, "支付宝账户配置不存在");
        BeanUtil.copyProperties(form, record);
        record.update();


    }

    /**
     * 批量删除
     *
     * @param idList id列表
     * @return int
     */
    public int delByIds(List<Long> idList) {

        return DB.deleteAll(FinanceAccountAlipay.class, idList);

    }


    public FinanceAccountAlipay getOneById(String appId) {
        FinanceAccountAlipay record = new QFinanceAccountAlipay().appId.eq(appId).setMaxRows(1).findOne();
        Assert.notNull(record, "支付宝账户配置不存在");
        return record;
    }
}
